package com.regentsoft.cloudback.enterpriseAdmin.controller;

import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyAdmin;
import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyAdminModel;
import com.regentsoft.cloudback.enterpriseAdmin.service.CompanyLoginService;
import com.regentsoft.cloudback.enterpriseAdmin.util.AdminTokenManager;
import com.regentsoft.cloudback.enterpriseAdmin.vo.ResultResp;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.CompanyLoginReq;
import com.regentsoft.cloudback.enterpriseAdmin.vo.resp.LoginResult;
import com.regentsoft.cloudback.exception.LoginException;
import com.regentsoft.cloudback.util.CodeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by work on 2017/11/27.
 *
 * @author lumr
 */
@RestController
@RequestMapping(value = "api/company/")
public class CompanyLoginController {
    @Autowired
    private AdminTokenManager tokenManager;
    @Autowired
    private CompanyLoginService loginService;

    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ApiOperation(value = "企业登录",notes = "企业登录",response = LoginResult.class,httpMethod = "POST")
    public ResultResp<LoginResult> login(@RequestBody CompanyLoginReq loginReq){
        CompanyAdmin admin;
        try {
            admin = loginService.login(loginReq);
        }catch (LoginException e){
            return new ResultResp<>(e);
        }

        String token = CodeUtil.genTOKEN();
        CompanyAdminModel adminModel = new CompanyAdminModel(admin);
        tokenManager.set(token,adminModel);

        return new ResultResp<>(new LoginResult(token, adminModel));
    }
}
